// components/cropper/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: false
        },
        width: {
            type: Number,
            value: 250
        },
        height: {
            type: Number,
            value: 250
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        windowHeight: wx.getSystemInfoSync().windowHeight,
        src: '',
        // width: 250, //宽度
        // height: 250, //高度
        max_width: 300,
        max_height: 300,
        disable_rotate: true, //是否禁用旋转
        disable_ratio: true, //锁定比例
        limit_move: true, //是否限制移动
    },
    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () { 
            
        },
        ready: function () {
            this.cropper = this.selectComponent("#image-cropper");
        },
        moved: function () { 
            //console.log("moved")
        },
        detached: function () { 
            //console.log("detached")
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        cropperload(e) {
            // console.log('cropper加载完成');
        },
        loadimage(e) {
            wx.hideLoading();
            // console.log('图片');
            this.cropper.imgReset();
        },
        clickcut(e) {
            // console.log(e.detail);
            //图片预览
            wx.previewImage({
                current: e.detail.url, // 当前显示图片的http链接
                urls: [e.detail.url] // 需要预览的图片http链接列表
            })
        },
        cancel(){
            this.triggerEvent('cancel');
        },
        upload() {
            let that = this;
            wx.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success(res) {
                    wx.showLoading({
                        title: '加载中',
                    })
                    const tempFilePaths = res.tempFilePaths[0];
                    //重置图片角度、缩放、位置
                    that.cropper.imgReset();
                    that.setData({
                        src: tempFilePaths
                    });
                }
            })
        },
        submit() {
            this.cropper.getImg((obj) => {
                this.setData({
                    src: ''
                })
                this.triggerEvent('upload', obj);
                // uploadFile({
                //     url: "/regist/uploadAvatar", 
                //     filePath: obj.url,
                //     name: "avatarFile",
                //     formData: {},
                // }).then(res => {
                //     if(res.code == 0){
                //         app.globalData.ecardRegistInfo.headUrl = res.data;
                //         wx.navigateBack()
                //     }
                // })                
            });
        },
    }
})
